var systemManageList = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

systemManageList.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj.find(".search-panel"));
			var bindFunctionArray = [{fName:"queryInfo",fs:this.findGridData},{fName:"resetInfo",fs:this.resetInfo}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.createGrid();
		},
		/*
		 * 创建空列表
		 */
		createGrid:function(){
			var columns = this.gridColumns();
			var that = this;
			this.sysUtils.createGird(this.targetObj.find("#systemMgList"), {
				dataSource:[],
				selectable: "single",
				reorderable: true,
				columnMenu: true,
				resizable: true,
				pageable: {input: true,
					numeric: false},
					columns: columns,
					dataBound:function(){
						that.targetObj.find("#systemMgList").find("tr:gt(0)").unbind("dblclick");
						that.targetObj.find("#systemMgList").find("tr:gt(0)").dblclick(function(e){
							that.openwindow(that.selectRow(),false);
						});	
					}
			});
		},
		/**
		 * 选中行
		 */
		selectRow:function(){
			var grid = this.targetObj.find("#systemMgList").data("kendoGrid");
			var dataItem = grid.dataItem(grid.select());
			if(isEmpty(dataItem)){
				return null;
			}
			var id = dataItem["id"];
			return id;
		},
		/**
		 * 打开子系统信息窗口
		 */
		openwindow:function(id,isEdit){
			//打开查看窗口
			var title = "添加子系统信息";
			this.editResult = null;
			var that = this;
			var opens = function(){
				that.isEdit = isEdit;
				that.openDivId = that.sysUtils.openWindow(that.openDivId,{
					title:title,
					height:"295px",
					width:"680px",
					resizable:false,
					content:"views/framework/systemManage/systemManage_Edit.html"
				}, true);
			};
			var loadData = function(){
				JsonpAjax(baseApiPath+"systemManage/queryById?systemId="+id,null,"json",function(result){
					that.editResult = result;
					opens();
				}); 
			}; 
			if(isNotEmpt(id) && false == isEdit){
				title = "查看子系统信息";
				loadData();
			}else if(isEmpty(id) && true == isEdit){
				title = "编辑子系统信息";
				id = that.selectRow();
				if(null == id){
					notification({ title:"提示信息",message:"请选择需要编辑的子系统信息!！"},'info');
					return;
				}
				loadData();
			}else{
				opens();
			}
		},
		/*
		 * 重置按钮
		 */
		resetInfo:function(e){
			var that = $(e.target).data("that");
			that.sysUtils.setBindFieldNull(that.bindBaseModels.bindFieldArray);
		},
		findGridData:function(e){
			var that = this;
			if(isNotEmpt(e)){
				that = $(e.target).data("that");
			}
			if(validityForm(that.targetObj)){
				var jsonObj = that.sysUtils.getBindDocJson();
				var kendoGrid  = that.targetObj.find("#systemMgList").data("kendoGrid");
				var urls = baseApiPath +"systemManage/queryWithPage";
				var dataT = {curdatetime:new Date().getTime(),json:JSON.stringify(jsonObj)};
				var dataSource = that.sysUtils.getDataSource({url:urls,data:dataT,dataType:"json"});
				console.log();
				kendoGrid.setDataSource(dataSource);
				that.targetObj.find("input[gridid]")[0].checked = false;
			}
		},
		/*
		 * 加载list 列表
		 */
		gridColumns:function(){
			return [{
				width:30,
				headerTemplate: '<input type="checkbox" gridId="systemMgList" onclick="gridCheckAll(this)" />',
				template: '<input name="childCheck" type="checkbox" value="${id}"/>'
			},{
				title: "子系统名称",
				field: "systemName"
			},{
				title: "子系统编号",
				field:"systenCode"
			},
			{
				title: "子系统状态",
				template:"#:Utils.getModelNameByParentKey(BASIS.DATA_STATUS.ROOT,systemStatus,Utils.systemType.SYS)#"
			},   
			{
				title: "创建时间",
				field: "createTime"
			},
			{
				title: "最后更新时间",
				field:"lastUpdateTime"
			}];
		},
		/**
		 * 删除子系统信息
		 */
		delSystem:function(){
			//获取呗选中的选择框
			var that = this;
			var valStr = "";
			that.targetObj.find("input[name='childCheck']:checked").each(function(){
				valStr += "'" + $(this).val() +"',";
			});
			if("" == valStr){
				notification({ title:"操作失败",message:"请勾选至少一条需要删除的记录!!"},'info');
				return;
			}
			notification({ title:"确认信息",message:"确定需要删除选中的子系统信息？"},'confirm',{callback:function(){
				valStr = valStr.substring(0, valStr.length - 1);
				JsonpAjax(baseApiPath+"systemManage/delSystemById?systemId="+valStr,"","json",function(result){
					that.findGridData();
				});
			}});
		},
		/**
		 * 更新子系统信息
		 */
		updateStatus:function(statusCode){
			var that = this;
			var statusName = null;
			if("Start" == statusCode){
				statusName = "启用";
			}else if("Stop" == statusCode){
				statusName = "停用";
			}else{
				notification({ title:"操作失败",message:"状态码异常，请重试!!"},'info');
				return;
			}
			//获取呗选中的选择框
			var valStr = "";
			this.targetObj.find("input[name='childCheck']:checked").each(function(){
				valStr += "'" + $(this).val() +"',";
			});
			if("" == valStr){
				notification({ title:"操作失败",message:"请勾选至少一条需要"+statusName+"的记录!!"},'info');
				return;
			}
			notification({ title:"确认信息",message:"确定需要"+statusName+"选中的子系统信息？"},'confirm',{callback:function(){
				valStr = valStr.substring(0, valStr.length - 1);
				JsonpAjax(baseApiPath+"systemManage/updateStatus?id="+valStr+"&statusCode="+statusCode,"","json",function(result){
					that.findGridData();
				});
			}});
		}
};



var systemManageEidt = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

systemManageEidt.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var bindFunctionArray = [{fName:"saveSystemInfo",fs:this.saveSystemInfo},{fName:"closeWindow",fs:closeWindow}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			Utils.createDataDictDropDownList({div:this.targetObj.find("input[data-bind='value:systemStatus']"),
				parentModelKey:BASIS.DATA_STATUS.ROOT},this,this.editFullData,Utils.systemType.SYS);
		},
		editFullData:function(){
			if(null != systemML.editResult){
				this.sysUtils.setBindFieldValue(systemML.editResult);
				//系统编号不允许随意更改
				this.targetObj.find("input[data-bind='value:systenCode']").attr("disabled","disabled");
				if(false == systemML.isEdit){ //查看模式
					this.targetObj.find("input").attr("disabled","disabled");
					this.targetObj.find("textarea").attr("disabled","disabled");
					this.targetObj.find(".save").remove();
				}
			}
		},
//		保存子系统信息
		saveSystemInfo:function(e){
			var that =  $(e.target).data("that");
			if(validityForm(that.targetObj)){
				var systemManageInfo = that.sysUtils.getBindDocJson();
				var url = baseApiPath+"systemManage/save";
				if(true == systemML.isEdit){
					url = baseApiPath+"systemManage/update";
					systemManageInfo["id"] = systemML.editResult["id"];
					systemManageInfo["createBy"] = systemML.editResult["createBy"];
					systemManageInfo["createTime"] = systemML.editResult["createTime"];
				}
				JsonpAjax(url,JSON.stringify(systemManageInfo),"json",function(result){
					closeWindow(); //关闭窗口
					systemML.findGridData();
				}); 
			}
		}
};